package com.example.rabbitmq_server.file;

/**
 * @author yifs
 * @version 1.0
 * @date 2024/3/28 10:34
 */

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class FileDownloader extends Application {

    private TextField urlTextField;
    private ProgressBar downloadProgress;
    private Label statusLabel;

    @Override
    public void start(Stage primaryStage) {
        // 输入框
        urlTextField = new TextField();
        urlTextField.setPromptText("请输入文件URL");

        // 下载按钮
        Button downloadButton = new Button("开始下载");
        downloadButton.setOnAction(e -> {
            String fileUrl = urlTextField.getText();
            downloadFile(fileUrl);
        });

        // 进度条
        downloadProgress = new ProgressBar(0);
        downloadProgress.setMaxWidth(Double.MAX_VALUE);

        // 状态标签
        statusLabel = new Label();

        // 布局
        HBox inputBox = new HBox(10, new Label("文件URL:"), urlTextField, downloadButton);
        VBox root = new VBox(10, inputBox, downloadProgress, statusLabel);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(20));

        // 设置场景并显示舞台
        Scene scene = new Scene(root, 600, 200);
        primaryStage.setScene(scene);
        primaryStage.setTitle("文件下载器");
        primaryStage.show();
    }

    private void downloadFile(String fileUrl) {
        String[] split = fileUrl.split("/");
        System.out.println(fileUrl);
        // 启动新线程进行下载
        Thread downloadThread = new Thread(() -> {
            try (BufferedInputStream in = new BufferedInputStream(openConnection(fileUrl).getInputStream())) {
                saveToFile(in, split[split.length - 1]); // 实现保存到文件的具体逻辑，这里只是示例
                updateProgress(1.0);
                // 将界面更新任务提交回FX应用线程执行
                Platform.runLater(() -> {
                    statusLabel.setText("下载完成！");
                });
            } catch (IOException ex) {
                updateProgress(-1);
                statusLabel.setText("下载失败！请检查URL和网络连接。");
                ex.printStackTrace();
            }
        });
        downloadThread.start();
    }

    private HttpURLConnection openConnection(String fileUrl) throws IOException {
        URL url = new URL(fileUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        return connection;
    }

    private void saveToFile(BufferedInputStream in, String name) throws IOException {

        // 如创建输出流并复制输入流内容到目标文件
        String url = "E:\\tianshu\\" + name;
        System.out.println(url);
        try (FileOutputStream out = new FileOutputStream(url)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
        }
    }

    private void updateProgress(double value) {
        Platform.runLater(() -> {
            downloadProgress.setProgress(value);
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
